package com.example.controller;


import com.example.common.Result;
import com.example.dto.AddressTDO;
import com.example.entity.Address;
import com.example.service.AddressService;
import com.example.vo.AddressVo;
import com.example.vo.DistrictVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("address")
@Api(tags = "地址模块")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @ApiOperation("获取默认地址")
    @GetMapping("/default/{userId}")
    public Result<Address> getDefaultAddress(@PathVariable Long userId) {
        Address defaultAddress = this.addressService.getDefaultAddress(userId);
        return Result.success(defaultAddress);
    }

    /**
     * 通过用户id获取该用户的地址信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("通过用户id获取该用户的地址信息")
    public Result getAddress(@Param("userId") Integer userId) {

        List<AddressVo> addressVoList = addressService.getAddressByUserId(userId);
        if (addressVoList == null) {
            return Result.error("1000", "地址不存在");
        }
        for (AddressVo addressVo : addressVoList) {
            addressVo.packAddressDetail();
        }
        return Result.success(addressVoList);

    }

    /**
     * 新增地址信息
     *
     * @param addressTDO
     * @return
     */
    @PostMapping("/new")
    @ApiOperation("新增地址信息")
    public Result addAddress(@RequestBody @Validated AddressTDO addressTDO) {
        addressService.addAddress(addressTDO);
        return Result.success();
    }

    /**
     * 根据地址id删除相应的地址
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation("根据地址id删除相应的地址")
    public Result deleteAddress(@Param("id") Integer id) {

        addressService.deleteAddressById(id);
        return Result.success();
    }

    /**
     * 修改地址信息
     *
     * @param addressTDO
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改地址信息")
    public Result updateAddress(@RequestBody @Validated AddressTDO addressTDO) {

        addressService.updateAddress(addressTDO);

        return Result.success();
    }

    /**
     * 设置地址为默认地址
     *
     * @param id
     * @param userId
     * @return
     */
    @PutMapping("/default/{id}/{userId}")
    @ApiOperation("设置地址为默认地址")
    public Result<Address> setDefault(
            @PathVariable("id") Integer id,
            @PathVariable("userId") Integer userId) {
        addressService.setDefault(id, userId);
        Address defaultAddress = this.addressService.getDefaultAddress(Long.valueOf(userId));
        return Result.success(defaultAddress);
    }

    /**
     * 根据地址id获取地址信息
     *
     * @param id
     * @return
     */
    @GetMapping("/addressById")
    @ApiOperation("根据地址id获取地址信息")
    public Result<AddressVo> getAddressById(@Param("id") Integer id) {

        return Result.success(addressService.getAddressById(id));
    }

    /**
     * 根据父类ID获取行政区地址信息
     *
     * @param parent
     * @return
     */
    @GetMapping("/parent")
    @ApiOperation("根据父类ID获取行政区地址信息")
    public Result<List<DistrictVo>> getDistrictByParentId(@Param("parent") Integer parent) {

        List<DistrictVo> districtList = addressService.getDistrictByParentId(parent);
        return Result.success(districtList);
    }

    /**
     * 获取展示订单页面地址
     *
     * @param userId
     * @return
     */
    @GetMapping("/orderAddress")
    @ApiOperation("获取展示订单页面地址")
    public Result<List<String>> getOrderAddress(@Param("userId") Integer userId) {

        List<String> orderAddress = addressService.getOrderAddress(userId);
        return Result.success(orderAddress);
    }

    @ApiOperation("根据id删除地址")
    @DeleteMapping("/{id}")
    public Result deleteOrderAddress(@PathVariable Integer id) {
        addressService.deleteAddressById(id);
        return Result.success();
    }
}
